<?php

/**
 * Invoice form base class.
 *
 * @method Invoice getObject() Returns the current form's model object
 *
 * @package    erpcrm
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
 */
abstract class BaseInvoiceForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'invoice_iden'         => new sfWidgetFormInputHidden(),
      'customer_iden'        => new sfWidgetFormPropelChoice(array('model' => 'Contact', 'add_empty' => false)),
      'invoice_type'         => new sfWidgetFormPropelChoice(array('model' => 'ListDocumentTypes', 'add_empty' => false, 'key_method' => 'getTypeid')),
      'invoice_id'           => new sfWidgetFormInputText(),
      'terms_name'           => new sfWidgetFormPropelChoice(array('model' => 'ListInvoiceTerms', 'add_empty' => false, 'key_method' => 'getTerm')),
      'days_due'             => new sfWidgetFormInputText(),
      'remarks'              => new sfWidgetFormTextarea(),
      'delivery_details'     => new sfWidgetFormTextarea(),
      'invoice_name'         => new sfWidgetFormInputText(),
      'delivery_mode'        => new sfWidgetFormInputText(),
      'delivery_date'        => new sfWidgetFormDate(),
      'vat_excempt_type'     => new sfWidgetFormInputText(),
      'payment_invoice_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Payment')),
    ));

    $this->setValidators(array(
      'invoice_iden'         => new sfValidatorPropelChoice(array('model' => 'Document', 'column' => 'document_iden', 'required' => false)),
      'customer_iden'        => new sfValidatorPropelChoice(array('model' => 'Contact', 'column' => 'iden')),
      'invoice_type'         => new sfValidatorPropelChoice(array('model' => 'ListDocumentTypes', 'column' => 'typeid')),
      'invoice_id'           => new sfValidatorString(array('max_length' => 15)),
      'terms_name'           => new sfValidatorPropelChoice(array('model' => 'ListInvoiceTerms', 'column' => 'term')),
      'days_due'             => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647)),
      'remarks'              => new sfValidatorString(),
      'delivery_details'     => new sfValidatorString(array('required' => false)),
      'invoice_name'         => new sfValidatorString(array('max_length' => 200)),
      'delivery_mode'        => new sfValidatorString(array('max_length' => 10)),
      'delivery_date'        => new sfValidatorDate(),
      'vat_excempt_type'     => new sfValidatorString(array('max_length' => 20)),
      'payment_invoice_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Payment', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('invoice[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Invoice';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['payment_invoice_list']))
    {
      $values = array();
      foreach ($this->object->getPaymentInvoices() as $obj)
      {
        $values[] = $obj->getPaymentIden();
      }

      $this->setDefault('payment_invoice_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->savePaymentInvoiceList($con);
  }

  public function savePaymentInvoiceList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['payment_invoice_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(PaymentInvoicePeer::INVOICE_IDEN, $this->object->getPrimaryKey());
    PaymentInvoicePeer::doDelete($c, $con);

    $values = $this->getValue('payment_invoice_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new PaymentInvoice();
        $obj->setInvoiceIden($this->object->getPrimaryKey());
        $obj->setPaymentIden($value);
        $obj->save();
      }
    }
  }

}
